package com.ruoyi.flowable.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.WfCategory;
import com.ruoyi.flowable.service.IWfCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 流程分类Controller
 *
 * @author siashan
 * @date 2022-12-29
 */
@RestController
@RequestMapping("/flowable/category")
public class WfCategoryController extends BaseController {

    @Autowired
    private IWfCategoryService wfCategoryService;

    /**
     * 查询流程分类列表
     *
     * @param wfCategory 参数
     * @return 列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(WfCategory wfCategory) {
        startPage();
        LambdaQueryWrapper<WfCategory> lqw = new LambdaQueryWrapper<WfCategory>();
        if (StringUtils.isNotBlank(wfCategory.getCategoryName())) {
            lqw.like(WfCategory::getCategoryName, wfCategory.getCategoryName());
        }
        if (StringUtils.isNotBlank(wfCategory.getCode())) {
            lqw.eq(WfCategory::getCode, wfCategory.getCode());
        }
        List<WfCategory> list = wfCategoryService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 查询流程分类列表
     *
     * @param wfCategory 参数
     * @return 列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:category:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(WfCategory wfCategory) {
        LambdaQueryWrapper<WfCategory> lqw = new LambdaQueryWrapper<WfCategory>();
        if (StringUtils.isNotBlank(wfCategory.getCategoryName())) {
            lqw.like(WfCategory::getCategoryName, wfCategory.getCategoryName());
        }
        if (StringUtils.isNotBlank(wfCategory.getCode())) {
            lqw.eq(WfCategory::getCode, wfCategory.getCode());
        }
        List<WfCategory> list = wfCategoryService.list(lqw);
        return AjaxResult.success(list);
    }

    /**
     * 导出流程分类列表
     *
     * @param wfCategory
     * @return 导出结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:category:export')")
    @Log(title = "流程分类导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WfCategory wfCategory) {
        LambdaQueryWrapper<WfCategory> lqw = new LambdaQueryWrapper<WfCategory>(wfCategory);
        List<WfCategory> list = wfCategoryService.list(lqw);
        ExcelUtil<WfCategory> util = new ExcelUtil<WfCategory>(WfCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 获取流程分类详细信息
     *
     * @param categoryId id
     * @return 详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(wfCategoryService.getById(categoryId));
    }

    /**
     * 新增流程分类
     *
     * @param wfCategory 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:category:add')")
    @Log(title = "流程分类新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WfCategory wfCategory) {
        if (!wfCategoryService.checkCategoryCodeUnique(wfCategory)) {
            return AjaxResult.error("新增流程分类'" + wfCategory.getCategoryName() + "'失败，流程编码已存在");
        }
        wfCategory.setCreateBy(getUsername());
        return toAjax(wfCategoryService.save(wfCategory));
    }

    /**
     * 修改流程分类
     *
     * @param wfCategory 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:category:edit')")
    @Log(title = "流程分类修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WfCategory wfCategory) {
        if (!wfCategoryService.checkCategoryCodeUnique(wfCategory)) {
            return AjaxResult.error("修改流程分类'" + wfCategory.getCategoryName() + "'失败，流程编码已存在");
        }
        return toAjax(wfCategoryService.updateById(wfCategory) ? 1 : 0);
    }

    /**
     * 删除流程分类
     *
     * @param categoryIds 主键id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('flowable:category:remove')")
    @Log(title = "流程分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(wfCategoryService.removeByIds(Arrays.asList(categoryIds)) ? 1 : 0);
    }
}
